package com.stage.battle
{
	import com.greensock.TweenLite;
	import com.resource.ResAsset;
	import com.resource.cache.BitmapCacher;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	
	public class HitsText extends Sprite
	{
		private var _container:Sprite = new Sprite;
		private var _tweenLite:TweenLite;
		private var _text:Bitmap = new Bitmap;
		private var _totalWidth:int = 0;
		private var _bmdHeight:int = 0;
		private var _scaleBig:Number = 2;
		private var _toBigTime:Number = 0.1;
		private var _toNormalTime:Number = 0.3;
		
		public function HitsText()
		{
			super();
			mouseChildren = false;
			mouseEnabled = false;
			addChild(_container);
			addChild(_text);
			
		}
		
		public function show(color:String, value:int):void{
			// 如果已经有显示，先清除
			while(_container.numChildren > 0){
				_container.removeChildAt(0);
			}
			if(_tweenLite){
				_tweenLite.kill();	
				_tweenLite = null;
			}
			
			var arr:Array = [];
			var str:String = value.toString();
			for(var i:int=0; i<str.length; i++){
				arr.push(color + str.charAt(i));
			}
			_text.bitmapData = BitmapCacher.getInstance().getBmdByClass(ResAsset.hits);
			startFlow(arr);
		}
		
		private function startFlow(arr:Array):void{
			var bmpArr:Array = [];
			_totalWidth = 0;
			for(var i:int=0; i<arr.length; i++){
				var bmp:Bitmap = new Bitmap;
				bmp.bitmapData = BitmapCacher.getInstance().getBmdByClass(ResAsset[arr[i]]);
				bmp.x = _totalWidth;
				_totalWidth += bmp.width;
				_bmdHeight = bmp.height;
				bmpArr.push(bmp);
				_container.addChild(bmp);
			}
			
			_container.y = 0;
			_container.x = 0;
			_container.scaleX = 0;
			_container.scaleY = 0;

			_tweenLite = TweenLite.to(_container, _toBigTime, {x:-_totalWidth*_scaleBig, scaleX:_scaleBig, scaleY:_scaleBig, onComplete:toBig});
		}
		
		private function toBig():void{
			_tweenLite = TweenLite.to(_container, _toNormalTime, {x:-_totalWidth, scaleX:1, scaleY:1, onComplete:toNormal});
		}
		
		private function toNormal():void{
			_tweenLite = null;
		}
	}
}